package javaSenior.threadExer;

/**
 * @author Lenl
 * @version v1.0
 * @create 2021-06-02 20:11
 * @description 创建两个分线程，其中一个遍历100内偶数，另一个遍历奇数
 */
public class ThreadDemo {
    public static void main(String[] args) {
        MyThread1 m1=new MyThread1();
        MyThread2 m2=new MyThread2();
        m1.setPriority(10);

        m1.start();
        m2.start();
        //只执行一次的可以采用匿名子类
        new Thread(){
            @Override
            public void run() {
                Thread.currentThread().setName("匿名线程");
                for (int i=0;i<100;i++){
                    if(i%2!=0){
                        System.out.println(Thread.currentThread().getName()+":"+ i);
                    }
                    if(i%20==0) {
                        this.yield();
                    }
                    if(i==21){
                        try {
                            m1.join();
                        } catch (InterruptedException e) {
                            System.out.println("join异常："+e.getMessage());
                        }
                    }
                }
            }
        }.start();

        System.out.println(m1.isAlive());
    }
}

class MyThread1 extends  Thread{
    @Override
    public void run() {
        for (int i=0;i<100;i++){
            if(i%2==0){
                System.out.println(Thread.currentThread().getName()+ i+"/"+getPriority());
            }
        }
    }
}

class MyThread2 extends  Thread{
    @Override
    public void run() {
        for (int i=0;i<100;i++){
            if(i==20) {
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println("sleep异常");
                }
            }
            if(i%2!=0){
                System.out.println(Thread.currentThread().getName()+":"+ i);
            }
        }
    }
}